home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / doc / ferm / ferm.html < prev    next >
Encoding:
Extensible Markup Language  |  2010-01-02  |  65.6 KB  |  1,884 lines

  1. <?xml version="1.0" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>B<ferm> - a firewall rule parser for linux</title>
  6. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  7. <link rev="made" href="mailto:root@localhost" />
  8. </head>
  9.  
  10. <body style="background-color: white">
  11.  
  12.  
  13. <!-- INDEX BEGIN -->
  14. <div name="index">
  15. <p><a name="__index__"></a></p>
  16.  
  17. <ul>
  18.  
  19.     <li><a href="#name">NAME</a></li>
  20.     <li><a href="#synopsis">SYNOPSIS</a></li>
  21.     <li><a href="#description">DESCRIPTION</a></li>
  22.     <li><a href="#caution">CAUTION</a></li>
  23.     <li><a href="#introduction">INTRODUCTION</a></li>
  24.     <li><a href="#structure_of_a_firewall_file">STRUCTURE OF A FIREWALL FILE</a></li>
  25.     <ul>
  26.  
  27.         <li><a href="#keywords">Keywords</a></li>
  28.         <li><a href="#parameters">Parameters</a></li>
  29.     </ul>
  30.  
  31.     <li><a href="#basic_keywords">BASIC KEYWORDS</a></li>
  32.     <ul>
  33.  
  34.         <li><a href="#location_keywords">Location keywords</a></li>
  35.         <li><a href="#basic_iptables_match_keywords">Basic iptables match keywords</a></li>
  36.         <li><a href="#basic_target_keywords">Basic target keywords</a></li>
  37.     </ul>
  38.  
  39.     <li><a href="#additional_keywords">ADDITIONAL KEYWORDS</a></li>
  40.     <ul>
  41.  
  42.         <li><a href="#iptables_match_modules">iptables match modules</a></li>
  43.         <li><a href="#iptables_target_modules">iptables target modules</a></li>
  44.     </ul>
  45.  
  46.     <li><a href="#other_domains">OTHER DOMAINS</a></li>
  47.     <ul>
  48.  
  49.         <li><a href="#arptables_keywords">arptables keywords</a></li>
  50.         <li><a href="#ebtables_keywords">ebtables keywords</a></li>
  51.     </ul>
  52.  
  53.     <li><a href="#advanced_features">ADVANCED FEATURES</a></li>
  54.     <ul>
  55.  
  56.         <li><a href="#variables">Variables</a></li>
  57.         <li><a href="#automatic_variables">Automatic variables</a></li>
  58.         <li><a href="#functions">Functions</a></li>
  59.         <li><a href="#backticks">Backticks</a></li>
  60.         <li><a href="#includes">Includes</a></li>
  61.         <li><a href="#conditionals">Conditionals</a></li>
  62.         <li><a href="#hooks">Hooks</a></li>
  63.     </ul>
  64.  
  65.     <li><a href="#built_in_functions">BUILT-IN FUNCTIONS</a></li>
  66.     <ul>
  67.  
  68.         <li><a href="#_resolve__hostname1_hostname2______">@resolve((hostname1 hostname2 ...))</a></li>
  69.     </ul>
  70.  
  71.     <li><a href="#recipes">RECIPES</a></li>
  72.     <ul>
  73.  
  74.         <li><a href="#easy_port_forwarding">Easy port forwarding</a></li>
  75.         <li><a href="#remote_ferm">Remote <strong>ferm</strong></a></li>
  76.     </ul>
  77.  
  78.     <li><a href="#options">OPTIONS</a></li>
  79.     <li><a href="#see_also">SEE ALSO</a></li>
  80.     <li><a href="#requirements">REQUIREMENTS</a></li>
  81.     <ul>
  82.  
  83.         <li><a href="#operating_system">Operating system</a></li>
  84.         <li><a href="#software">Software</a></li>
  85.     </ul>
  86.  
  87.     <li><a href="#bugs">BUGS</a></li>
  88.     <li><a href="#copyright">COPYRIGHT</a></li>
  89.     <li><a href="#author">AUTHOR</a></li>
  90. </ul>
  91.  
  92. <hr name="index" />
  93. </div>
  94. <!-- INDEX END -->
  95.  
  96. <p>
  97. </p>
  98. <h1><a name="name">NAME</a></h1>
  99. <p><strong>ferm</strong> - a firewall rule parser for linux</p>
  100. <p>
  101. </p>
  102. <hr />
  103. <h1><a name="synopsis">SYNOPSIS</a></h1>
  104. <p><strong>ferm</strong> <em>options</em> <em>inputfile</em></p>
  105. <p>
  106. </p>
  107. <hr />
  108. <h1><a name="description">DESCRIPTION</a></h1>
  109. <p><strong>ferm</strong> is a frontend for <strong>iptables</strong>. It reads the rules from a
  110. structured configuration file and calls <code>iptables(8)</code> to insert them
  111. into the running kernel.</p>
  112. <p><strong>ferm</strong>'s goal is to make firewall rules easy to write and easy to
  113. read. It tries to reduce the tedious task of writing down rules, thus
  114. enabling the firewall administrator to spend more time on developing
  115. good rules than the proper implementation of the rule.</p>
  116. <p>To achieve this, <strong>ferm</strong> uses a simple but powerful configuration
  117. language, which allows variables, functions, arrays, blocks. It also
  118. allows you to include other files, allowing you to create libraries of
  119. commonly used structures and functions.</p>
  120. <p><strong>ferm</strong>, pronounced "firm", stands for "For Easy Rule Making".</p>
  121. <p>
  122. </p>
  123. <hr />
  124. <h1><a name="caution">CAUTION</a></h1>
  125. <p>This manual page does <em>not</em> indend to teach you how firewalling works
  126. and how to write good rules.  There is already enough documentation on
  127. this topic.</p>
  128. <p>
  129. </p>
  130. <hr />
  131. <h1><a name="introduction">INTRODUCTION</a></h1>
  132. <p>Let's start with a simple example:</p>
  133. <pre>
  134.     chain INPUT {
  135.         proto tcp ACCEPT;
  136.     }</pre>
  137. <p>This will add a rule to the predefined input chain, matching and
  138. accepting all tcp packets.  Ok, let's make it more complicated:</p>
  139. <pre>
  140.     chain (INPUT OUTPUT) {
  141.         proto (udp tcp) ACCEPT;
  142.     }</pre>
  143. <p>This will insert 4 rules, namely 2 in chain input, and 2 in chain
  144. output, matching and accepting both udp and tcp packets.  Normally you
  145. would type this:</p>
  146. <pre>
  147.    iptables -A INPUT -p tcp -j ACCEPT
  148.    iptables -A OUTPUT -p tcp -j ACCEPT
  149.    iptables -A INPUT -p udp -j ACCEPT
  150.    iptables -A OUTPUT -p udp -j ACCEPT</pre>
  151. <p>Note how much less typing we need to do? :-)</p>
  152. <p>Basically, this is all there is to it, although you can make it quite
  153. more complex. Something to look at:</p>
  154. <pre>
  155.    chain INPUT {
  156.        policy ACCEPT;
  157.        daddr 10.0.0.0/8 proto tcp dport ! ftp jump mychain sport :1023 TOS 4 settos 8 mark 2;
  158.        daddr 10.0.0.0/8 proto tcp dport ftp REJECT;
  159.    }</pre>
  160. <p>My point here is, that *you* need to make nice rules, keep
  161. them readable to you and others, and not make it into a mess.</p>
  162. <p>It would aid the reader if the resulting firewall rules were placed
  163. here for reference. Also, you could include the nested version with
  164. better readability.</p>
  165. <p>Try using comments to show what you are doing:</p>
  166. <pre>
  167.     # this line enables transparent http-proxying for the internal network:
  168.     proto tcp if eth0 daddr ! 192.168.0.0/255.255.255.0
  169.         dport http REDIRECT to-ports 3128;</pre>
  170. <p>You will be thankful for it later!</p>
  171. <pre>
  172.     chain INPUT {
  173.         policy ACCEPT;
  174.         interface (eth0 ppp0) {
  175.             # deny access to notorius hackers, return here if no match
  176.             # was found to resume normal firewalling
  177.             jump badguys;</pre>
  178. <pre>
  179.             protocol tcp jump fw_tcp;
  180.             protocol udp jump fw_udp;
  181.         }
  182.     }</pre>
  183. <p>The more you nest, the better it looks. Make sure the order you
  184. specify is correct, you would not want to do this:</p>
  185. <pre>
  186.     chain FORWARD {
  187.         proto ! udp DROP;
  188.         proto tcp dport ftp ACCEPT;
  189.     }</pre>
  190. <p>because the second rule will never match. Best way is to specify
  191. first everyting that is allowed, and then deny everything else.
  192. Look at the examples for more good snapshots. Most people do
  193. something like this:</p>
  194. <pre>
  195.     proto tcp {
  196.         dport (
  197.             ssh http ftp
  198.         ) ACCEPT;
  199.         dport 1024:65535 ! syn ACCEPT;
  200.         DROP;
  201.     }</pre>
  202. <p>
  203. </p>
  204. <hr />
  205. <h1><a name="structure_of_a_firewall_file">STRUCTURE OF A FIREWALL FILE</a></h1>
  206. <p>The structure of a proper firewall file looks like  simplified
  207. C-code. Only a few syntactic characters are used in ferm-
  208. configuration files. Besides these special caracters, ferm
  209. uses 'keys' and 'values', think of them as options and
  210. parameters, or as variables and values, whatever.</p>
  211. <p>With these words, you define the characteristics of your firewall.
  212. Every firewall consists of two things: First, look if network
  213. traffic matches certain conditions, and second, what to do
  214. with that traffic.</p>
  215. <p>You may specify conditions that are valid for the kernel
  216. interface program you are using, probably <code>iptables(8)</code>. For
  217. instance, in iptables, when you are trying to match tcp
  218. packets, you would say:</p>
  219. <pre>
  220.     iptables --protocol tcp</pre>
  221. <p>In ferm, this will become:</p>
  222. <pre>
  223.     protocol tcp;</pre>
  224. <p>Just typing this in ferm doesn't do anything, you need to tell
  225. ferm (actually, you need to tell <code>iptables(8)</code> and the kernel) what
  226. to do with any traffic that matches this condition:</p>
  227. <pre>
  228.     iptables --protocol tcp -j ACCEPT</pre>
  229. <p>Or, translated to <strong>ferm</strong>:</p>
  230. <pre>
  231.     protocol tcp ACCEPT;</pre>
  232. <p>The <strong>;</strong> character is at the end of every ferm rule. Ferm ignores line
  233. breaks, meaning the above example is identical to the following:</p>
  234. <pre>
  235.     protocol tcp
  236.       ACCEPT;</pre>
  237. <p>Here's a list of the special characters:</p>
  238. <dl>
  239. <dt><strong><a name="_" class="item"><strong>;</strong></a></strong></dt>
  240.  
  241. <dd>
  242. <p>This character finalizes a rule.</p>
  243. <p>Separated by semicolons, you may write multiple rules in one line,
  244. although this decreases readability:</p>
  245. <pre>
  246.     protocol tcp ACCEPT; protocol udp DROP;</pre>
  247. </dd>
  248. <dt><strong><a name="__" class="item"><strong>{}</strong></a></strong></dt>
  249.  
  250. <dd>
  251. <p>The nesting symbol defines a 'block' of rules.</p>
  252. <p>The curly brackets contain any number of nested rules. All matches
  253. before the block are carried forward to these.</p>
  254. <p>The closing curly bracket finalizes the rule set. You should not write
  255. a ';' after that, because that would be an empty rule.</p>
  256. <p>Example:</p>
  257. <pre>
  258.     chain INPUT proto icmp {
  259.         icmp-type echo-request ACCEPT;
  260.         DROP;
  261.     }</pre>
  262. <p>This block shows two rules inside a block, which will both be merged
  263. with anything in front of it, so you will get two rules:</p>
  264. <pre>
  265.     iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  266.     iptables -A INPUT -p icmp -j DROP</pre>
  267. <p>There can be multiple nesting levels:</p>
  268. <pre>
  269.     chain INPUT {
  270.         proto icmp {
  271.             icmp-type echo-request ACCEPT;
  272.             DROP;
  273.         }
  274.         daddr 172.16.0.0/12 REJECT;
  275.     }</pre>
  276. <p>Note that the 'REJECT' rule is not affected by 'proto icmp', although
  277. there is no ';' after the closing curly brace. Translated to iptables:</p>
  278. <pre>
  279.     iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  280.     iptables -A INPUT -p icmp -j DROP
  281.     iptables -A INPUT -d 172.16.0.0/12 -j REJECT</pre>
  282. </dd>
  283. <dt><strong><a name="_" class="item"><strong>$</strong></a></strong></dt>
  284.  
  285. <dd>
  286. <p>Variable expansion. Replaces '$FOO' by the value of the variable. See
  287. the section <em>VARIABLES</em> for details.</p>
  288. </dd>
  289. <dt><strong><a name="_" class="item"><strong>&</strong></a></strong></dt>
  290.  
  291. <dd>
  292. <p>Function call. See the section <em>FUNCTIONS</em> for details.</p>
  293. </dd>
  294. <dt><strong><a name="__" class="item"><strong>()</strong></a></strong></dt>
  295.  
  296. <dd>
  297. <p>The array symbol. Using the parentheses, you can define
  298. a 'list' of values that should be applied for the key to the
  299. left of it.</p>
  300. <p>Example:</p>
  301. <pre>
  302.     protocol ( tcp udp icmp )</pre>
  303. <p>this will result in three rules:</p>
  304. <pre>
  305.     ... -p tcp ...
  306.     ... -p udp ...
  307.     ... -p icmp ...</pre>
  308. <p>Only values can be 'listed', so you cannot do something like this:</p>
  309. <pre>
  310.     proto tcp ( ACCEPT LOG );</pre>
  311. <p>but you can do this:</p>
  312. <pre>
  313.     chain (INPUT OUTPUT FORWARD) proto (icmp udp tcp) DROP;</pre>
  314. <p>(which will result in nine rules!)</p>
  315. <p>Values are separated by spaces. The array symbol is both left- and
  316. right-associative, in contrast with the nesting block, which is
  317. left-associative only.</p>
  318. </dd>
  319. <dt><strong><a name="_" class="item"><code> # </code></a></strong></dt>
  320.  
  321. <dd>
  322. <p>The comment symbol. Anything that follows this symbol up to
  323. the end of line is ignored.</p>
  324. </dd>
  325. <dt><strong><a name="command" class="item"><code>`command`</code></a></strong></dt>
  326.  
  327. <dd>
  328. <p>Execute the command in a shell, and insert the process output. See the
  329. section <em>backticks</em> for details.</p>
  330. </dd>
  331. <dt><strong><a name="string" class="item"><code>'string'</code></a></strong></dt>
  332.  
  333. <dd>
  334. <p>Quote a string which may contain whitespaces, the dollar sign etc.</p>
  335. <pre>
  336.     LOG log-prefix ' hey, this is my log prefix!';</pre>
  337. </dd>
  338. <dt><strong><a name="string2" class="item"><a href="#string"><code>"string"</code></a></a></strong></dt>
  339.  
  340. <dd>
  341. <p>Quote a string (see above), but variable references with a dollar sign
  342. are evaluated:</p>
  343. <pre>
  344.     DNAT to "$myhost:$myport";</pre>
  345. </dd>
  346. </dl>
  347. <p>
  348. </p>
  349. <h2><a name="keywords">Keywords</a></h2>
  350. <p>In the previous section, we already introduced some basic keywords
  351. like "chain", "protocol" and "ACCEPT". Let's explore their nature.</p>
  352. <p>There are three kinds of keywords:</p>
  353. <dl>
  354. <dt>
  355. <dd>
  356. <p><strong>location</strong> keywords define where a rule will be created. Example:
  357. "table", "chain".</p>
  358. </dd>
  359. <dt>
  360. <dd>
  361. <p><strong>match</strong> keywords perform a test on all passing packets. The current
  362. rule is without effect if one (or more) of the matches does not
  363. pass. Example: "proto", "daddr".</p>
  364. <p>Most matches are followed by a parameter: "proto tcp", "daddr
  365. 172.16.0.0/12".</p>
  366. </dd>
  367. <dt>
  368. <dd>
  369. <p><strong>target</strong> keywords state what to do with a packet. Example: "ACCEPT",
  370. "REJECT", "jump".</p>
  371. <p>Some targets define more keywords to specify details: "REJECT
  372. reject-with icmp-net-unreachable".</p>
  373. </dd>
  374. </dl>
  375. <p>Every rule consists of a <strong>location</strong> and a <strong>target</strong>, plus any number
  376. of <strong>matches</strong>:</p>
  377. <pre>
  378.     table filter                  # location
  379.     proto tcp dport (http https)  # match
  380.     ACCEPT;                       # target</pre>
  381. <p>Strictly speaking, there is a fourth kind: <strong>ferm</strong> keywords (which
  382. control ferm's internal behaviour), but they will be explained later.</p>
  383. <p>
  384. </p>
  385. <h2><a name="parameters">Parameters</a></h2>
  386. <p>Many keywords take parameters. These can be specified as literals,
  387. variable references or lists (arrays):</p>
  388. <pre>
  389.     proto udp
  390.     saddr %TRUSTED_HOSTS;
  391.     proto tcp dport (http https ssh);
  392.     LOG log-prefix "funky wardriver alert: ";</pre>
  393. <p>Some of them can be negated (lists cannot be negated):</p>
  394. <pre>
  395.     proto !esp;
  396.     proto udp dport !domain;</pre>
  397. <p>Keywords which take no parameters are negated by a prefixed '!':</p>
  398. <pre>
  399.     proto tcp !syn;</pre>
  400. <p>Read <code>iptables(8)</code> to see where the <strong>!</strong> can be used.</p>
  401. <p>
  402. </p>
  403. <hr />
  404. <h1><a name="basic_keywords">BASIC KEYWORDS</a></h1>
  405. <p>
  406. </p>
  407. <h2><a name="location_keywords">Location keywords</a></h2>
  408. <dl>
  409. <dt><strong><a name="domain_ip_ip6" class="item"><strong>domain [ip|ip6]</strong></a></strong></dt>
  410.  
  411. <dd>
  412. <p>Set the domain. "ip" is default and means "IPv4" (iptables). "ip6" is
  413. for IPv6 support, using "ip6tables".</p>
  414. </dd>
  415. <dt><strong><a name="table_filter_nat_mangle" class="item"><strong>table [filter|nat|mangle]</strong></a></strong></dt>
  416.  
  417. <dd>
  418. <p>Specifies which netfilter table this rule will be inserted to:
  419. "filter" (default), "nat" or "mangle".</p>
  420. </dd>
  421. <dt><strong><a name="chain_chain_name" class="item"><strong>chain [chain-name]</strong></a></strong></dt>
  422.  
  423. <dd>
  424. <p>Specifies the netfilter chain (within the current table) this rule
  425. will be inserted to. Common predefined chain names are "INPUT",
  426. "OUTPUT", "FORWARD", "PREROUTING", "POSTROUTING", depending on the
  427. table. See the netfilter documentation for details.</p>
  428. <p>If you specify a non-existing chain here, ferm will add the rule to a
  429. custom chain with that name.</p>
  430. </dd>
  431. <dt><strong><a name="policy_accept_drop" class="item"><strong>policy [ACCEPT|DROP|..]</strong></a></strong></dt>
  432.  
  433. <dd>
  434. <p>Specifies the default policy for the current chain (built-in
  435. only). Can be one of the built-in targets (ACCEPT, DROP, REJECT,
  436. ...). A packet that matches no rules in a chain will be treated as
  437. specified by the policy.</p>
  438. <p>To avoid ambiguity, always specify the policies of all predefined
  439. chains explicitly.</p>
  440. </dd>
  441. <dt><strong><a name="_subchain" class="item"><strong>@subchain ["CHAIN-NAME"] { ... }</strong></a></strong></dt>
  442.  
  443. <dd>
  444. <p>Works like the normal block operators (i.e. without the <em>@subchain</em>
  445. keyword), except that <strong>ferm</strong> moves rules within the curly braces into
  446. a new custom chain. The name for this chain is chosen automatically by
  447. ferm.</p>
  448. <p>In many cases, this is faster than just a block, because the kernel
  449. may skip a huge block of rules when a precondition is false. Imagine
  450. the following example:</p>
  451. <pre>
  452.  table filter chain INPUT {
  453.      saddr (1.2.3.4 2.3.4.5 3.4.5.6 4.5.6.7 5.6.7.8) {
  454.          proto tcp dport (http https ssh) ACCEPT;
  455.          proto udp dport domain ACCEPT;
  456.      }
  457.  }</pre>
  458. <p>This generates 20 rules. When a packet arrives which does not pass the
  459. <strong>saddr</strong> match, it nonetheless checks all 20 rules. With <strong>@subchain</strong>,
  460. this check is done once, resulting in faster network filtering and
  461. less CPU load:</p>
  462. <pre>
  463.  table filter chain INPUT {
  464.      saddr (1.2.3.4 2.3.4.5 3.4.5.6 4.5.6.7 5.6.7.8) @subchain {
  465.          proto tcp dport (http https ssh) ACCEPT;
  466.          proto udp dport domain ACCEPT;
  467.      }
  468.  }</pre>
  469. <p>Optionally, you may define the name of the sub chain:</p>
  470. <pre>
  471.  saddr (1.2.3.4 2.3.4.5 3.4.5.6) @subchain "foobar" {
  472.      proto tcp dport (http https ssh) ACCEPT;
  473.      proto udp dport domain ACCEPT;
  474.  }</pre>
  475. <p>You can achieve the same by explicitly declaring a custom chain, but
  476. you may feel that using <strong>@subchain</strong> requires less typing.</p>
  477. </dd>
  478. </dl>
  479. <p>
  480. </p>
  481. <h2><a name="basic_iptables_match_keywords">Basic iptables match keywords</a></h2>
  482. <dl>
  483. <dt><strong><a name="interface_interface_name" class="item"><strong>interface [interface-name]</strong></a></strong></dt>
  484.  
  485. <dd>
  486. <p>Define the interface name, your outside network card, like eth0,
  487. or dialup like ppp1, or whatever device you want to match for
  488. passing packets. It is equivalent to the <code>-i</code> switch in
  489. <code>iptables(8)</code>.</p>
  490. </dd>
  491. <dt><strong><a name="outerface_interface_name" class="item"><strong>outerface [interface-name]</strong></a></strong></dt>
  492.  
  493. <dd>
  494. <p>Same as interface, only for matching the outgoing interface
  495. for a packet, as in <code>iptables(8)</code>.</p>
  496. </dd>
  497. <dt><strong><a name="protocol_protocol_name_protocol_number" class="item"><strong>protocol [protocol-name|protocol-number]</strong></a></strong></dt>
  498.  
  499. <dd>
  500. <p>Currently supported by the kernel are tcp, udp and icmp, or
  501. their respective numbers.</p>
  502. </dd>
  503. <dt><strong><a name="saddr_daddr_address_spec" class="item"><strong>saddr|daddr [address-spec]</strong></a></strong></dt>
  504.  
  505. <dd>
  506. <p>Matches on packets originating from the specified address (saddr) or
  507. targeted at the address (daddr).</p>
  508. <p>Examples:</p>
  509. <pre>
  510.     saddr 192.168/8 ACCEPT; # (identical to the next one:)
  511.     saddr 192.168.0.0/255.255.255.0 ACCEPT;
  512.     daddr my.domain.com ACCEPT;</pre>
  513. </dd>
  514. <dt><strong><a name="fragment" class="item"><strong>fragment</strong></a></strong></dt>
  515.  
  516. <dd>
  517. <p>Specify that only fragmented IP packets should be matched.
  518. When packets are larger that the maximum packet size your
  519. system can handle (called Maximum Transmission Unit or MTU)
  520. they will be chopped into bits and sent one by one as single
  521. packets. See <code>ifconfig(8)</code> if you want to find the MTU for
  522. your system (the default is usually 1500 bytes).</p>
  523. <p>Fragments are frequently used in DOS attacks, because there
  524. is no way of finding out the origin of a fragment packet.</p>
  525. </dd>
  526. <dt><strong><a name="sport_dport_port_spec" class="item"><strong>sport|dport [port-spec]</strong></a></strong></dt>
  527.  
  528. <dd>
  529. <p>Matches on packets on the specified TCP or UDP port. "sport" matches
  530. the source port, and dport matches the destination port.</p>
  531. <p>This match can be used only after you specified "protocol tcp" or
  532. "protocol udp", because only these two protocols actually have ports.</p>
  533. <p>And some examples of valid ports/ranges:</p>
  534. <pre>
  535.     dport 80 ACCEPT;
  536.     dport http ACCEPT;
  537.     dport ssh:http ACCEPT;
  538.     dport 0:1023 ACCEPT; # equivalent to :1023
  539.     dport 1023:65535 ACCEPT;</pre>
  540. </dd>
  541. <dt><strong><a name="syn" class="item"><strong>syn</strong></a></strong></dt>
  542.  
  543. <dd>
  544. <p>Specify that the SYN flag in a tcp package should be matched,
  545. which are used to build new tcp connections. You can identify
  546. incoming connections with this, and decide wether you want
  547. to allow it or not. Packets that do not have this flag are
  548. probably from an already established connection, so it's
  549. considered reasonably safe to let these through.</p>
  550. </dd>
  551. <dt><strong><a name="module_module_name" class="item"><strong>module [module-name]</strong></a></strong></dt>
  552.  
  553. <dd>
  554. <p>Load an iptables module. Most modules provide more match
  555. keywords. We'll get to that later.</p>
  556. </dd>
  557. </dl>
  558. <p>
  559. </p>
  560. <h2><a name="basic_target_keywords">Basic target keywords</a></h2>
  561. <dl>
  562. <dt><strong><a name="jump_custom_chain_name" class="item"><strong>jump [custom-chain-name]</strong></a></strong></dt>
  563.  
  564. <dd>
  565. <p>Jumps to a custom chain. If no rule in the custom chain matched,
  566. netfilter returns to the next rule in the previous chain.</p>
  567. </dd>
  568. <dt><strong><a name="realgoto_custom_chain_name" class="item"><strong>realgoto [custom-chain-name]</strong></a></strong></dt>
  569.  
  570. <dd>
  571. <p>Go to a custom chain.  Unlike the <strong>jump</strong> option, <strong>RETURN</strong> will not
  572. continue processing in this chain but instead in the chain that called
  573. us via <strong>jump</strong>.</p>
  574. <p>The keyword <strong>realgoto</strong> was chosen during the transition period,
  575. because <strong>goto</strong> (already deprecated) used to be an alias for <strong>jump</strong>.</p>
  576. </dd>
  577. <dt><strong><a name="accept" class="item"><strong>ACCEPT</strong></a></strong></dt>
  578.  
  579. <dd>
  580. <p>Accepts matching packets.</p>
  581. </dd>
  582. <dt><strong><a name="drop" class="item"><strong>DROP</strong></a></strong></dt>
  583.  
  584. <dd>
  585. <p>Drop matching packets without further notice.</p>
  586. </dd>
  587. <dt><strong><a name="reject" class="item"><strong>REJECT</strong></a></strong></dt>
  588.  
  589. <dd>
  590. <p>Rejects matching packets, i.e. send an ICMP packet to the sender,
  591. which is port-unreachable by default. You may specify another ICMP
  592. type.</p>
  593. <pre>
  594.     REJECT; # default to icmp-port-unreachable
  595.     REJECT reject-with icmp-net-unreachable;</pre>
  596. <p>Type "iptables -j REJECT -h" for details.</p>
  597. </dd>
  598. <dt><strong><a name="return" class="item"><strong>RETURN</strong></a></strong></dt>
  599.  
  600. <dd>
  601. <p>Finish the current chain and return to the calling chain (if "jump
  602. [custom-chain-name]" was used).</p>
  603. </dd>
  604. <dt><strong><a name="nop" class="item"><strong>NOP</strong></a></strong></dt>
  605.  
  606. <dd>
  607. <p>No action at all.</p>
  608. </dd>
  609. </dl>
  610. <p>
  611. </p>
  612. <hr />
  613. <h1><a name="additional_keywords">ADDITIONAL KEYWORDS</a></h1>
  614. <p>Netfilter is modular. Modules may provide additional targets and match
  615. keywords. The list of netfilter modules is constantly growing, and
  616. ferm tries to keep up with supporting them all. This chapter describes
  617. modules which are currently supported.</p>
  618. <p>
  619. </p>
  620. <h2><a name="iptables_match_modules">iptables match modules</a></h2>
  621. <dl>
  622. <dt><strong><a name="account" class="item"><strong>account</strong></a></strong></dt>
  623.  
  624. <dd>
  625. <p>Account traffic for all hosts in defined network/netmask.  This is one
  626. of the match modules which behave like a target, i.e. you will mostly
  627. have to use the <strong>NOP</strong> target.</p>
  628. <pre>
  629.     mod account aname mynetwork aaddr 192.168.1.0/24 ashort NOP;</pre>
  630. </dd>
  631. <dt><strong><a name="addrtype" class="item"><strong>addrtype</strong></a></strong></dt>
  632.  
  633. <dd>
  634. <p>Check the address type; either source address or destination address.</p>
  635. <pre>
  636.     mod addrtype src-type BROADCAST;
  637.     mod addrtype dst-type LOCAL;</pre>
  638. <p>Type "iptables -m addrtype -h" for details.</p>
  639. </dd>
  640. <dt><strong><a name="ah" class="item"><strong>ah</strong></a></strong></dt>
  641.  
  642. <dd>
  643. <p>Checks the SPI header in an AH packet.</p>
  644. <pre>
  645.     mod ah ahspi 0x101;
  646.     mod ah ahspi ! 0x200:0x2ff;</pre>
  647. <p>Additional arguments for IPv6:</p>
  648. <pre>
  649.     mod ah ahlen 32 ACCEPT;
  650.     mod ah ahlen !32 ACCEPT;
  651.     mod ah ahres ACCEPT;</pre>
  652. </dd>
  653. <dt><strong><a name="comment" class="item"><strong>comment</strong></a></strong></dt>
  654.  
  655. <dd>
  656. <p>Adds a comment of up to 256 characters to a rule, without an effect.
  657. Note that unlike ferm comments ('#'), this one will show up in
  658. "iptables -L".</p>
  659. <pre>
  660.     mod comment comment "This is my comment." ACCEPT;</pre>
  661. </dd>
  662. <dt><strong><a name="condition" class="item"><strong>condition</strong></a></strong></dt>
  663.  
  664. <dd>
  665. <p>Matches if a value in /proc/net/ipt_condition/NAME is 1 (path is
  666. /proc/net/ip6t_condition/NAME for the ip6 domain).</p>
  667. <pre>
  668.     mod condition condition (abc def) ACCEPT;
  669.     mod condition condition !foo ACCEPT;</pre>
  670. </dd>
  671. <dt><strong><a name="connbytes" class="item"><strong>connbytes</strong></a></strong></dt>
  672.  
  673. <dd>
  674. <p>Match by how many bytes or packets a connection (or one of the two
  675. flows constituting the connection) have tranferred so far, or by
  676. average bytes per packet.</p>
  677. <pre>
  678.     mod connbytes connbytes 65536: connbytes-dir both connbytes-mode bytes ACCEPT;
  679.     mod connbytes connbytes !1024:2048 connbytes-dir reply connbytes-mode packets ACCEPT;</pre>
  680. <p>Valid values for <em>connbytes-dir</em>: <em>original</em>, <em>reply</em>, <em>both</em>; for
  681. <em>connbytes-mode</em>: <em>packets</em>, <em>bytes</em>, <em>avgpkt</em>.</p>
  682. </dd>
  683. <dt><strong><a name="connlimit" class="item"><strong>connlimit</strong></a></strong></dt>
  684.  
  685. <dd>
  686. <p>Allows you to restrict the number of parallel TCP connections to a
  687. server per client IP address (or address block).</p>
  688. <pre>
  689.     mod connlimit connlimit-above 4 REJECT;
  690.     mod connlimit connlimit-above !4 ACCEPT;
  691.     mod connlimit connlimit-above 4 connlimit-mask 24 REJECT;</pre>
  692. </dd>
  693. <dt><strong><a name="connmark" class="item"><strong>connmark</strong></a></strong></dt>
  694.  
  695. <dd>
  696. <p>Check the mark field associated with the connection, set by the
  697. CONNMARK target.</p>
  698. <pre>
  699.     mod connmark mark 64;
  700.     mod connmark mark 6/7;</pre>
  701. </dd>
  702. <dt><strong><a name="conntrack" class="item"><strong>conntrack</strong></a></strong></dt>
  703.  
  704. <dd>
  705. <p>Check connection tracking information.</p>
  706. <pre>
  707.     mod conntrack ctstate (ESTABLISHED RELATED);
  708.     mod conntrack ctproto tcp;
  709.     mod conntrack ctorigsrc 192.168.0.2;
  710.     mod conntrack ctorigdst 1.2.3.0/24;
  711.     mod conntrack ctreplsrc 2.3.4.5;
  712.     mod conntrack ctrepldst ! 3.4.5.6;
  713.     mod conntrack ctstatus ASSURED;
  714.     mod conntrack ctexpire 60;
  715.     mod conntrack ctexpire 180:240;</pre>
  716. <p>Type "iptables -m conntrack -h" for details.</p>
  717. </dd>
  718. <dt><strong><a name="dccp" class="item"><strong>dccp</strong></a></strong></dt>
  719.  
  720. <dd>
  721. <p>Check DCCP (Datagram Congestion Control Protocol) specific attributes.
  722. This module is automatically loaded when you use "protocol dccp".</p>
  723. <pre>
  724.     proto dccp sport 1234 dport 2345 ACCEPT;
  725.     proto dccp dccp-types (SYNCACK ACK) ACCEPT;
  726.     proto dccp dccp-types !REQUEST DROP;
  727.     proto dccp dccp-option 2 ACCEPT;</pre>
  728. </dd>
  729. <dt><strong><a name="dscp" class="item"><strong>dscp</strong></a></strong></dt>
  730.  
  731. <dd>
  732. <p>Match the 6 bit DSCP field within the TOS field.</p>
  733. <pre>
  734.     mod dscp dscp 11;
  735.     mod dscp dscp-class AF41;</pre>
  736. </dd>
  737. <dt><strong><a name="dst" class="item"><strong>dst</strong></a></strong></dt>
  738.  
  739. <dd>
  740. <p>Matches the Destination Options header (ip6).</p>
  741. <pre>
  742.     mod dst dst-len 8 ACCEPT;
  743.     mod dst dst-len !8 ACCEPT;
  744.     mod dst dst-opts (1:4 2:8) ACCEPT;</pre>
  745. </dd>
  746. <dt><strong><a name="ecn" class="item"><strong>ecn</strong></a></strong></dt>
  747.  
  748. <dd>
  749. <p>Match the ECN bits of an IPv4 TCP header.</p>
  750. <pre>
  751.     mod ecn ecn-tcp-cwr;
  752.     mod ecn ecn-tcp-ece;
  753.     mod ecn ecn-ip-ect 2;</pre>
  754. <p>Type "iptables -m ecn -h" for details.</p>
  755. </dd>
  756. <dt><strong><a name="esp" class="item"><strong>esp</strong></a></strong></dt>
  757.  
  758. <dd>
  759. <p>Checks the SPI header in an ESP packet.</p>
  760. <pre>
  761.     mod esp espspi 0x101;
  762.     mod esp espspi ! 0x200:0x2ff;</pre>
  763. </dd>
  764. <dt><strong><a name="eui64" class="item"><strong>eui64</strong></a></strong></dt>
  765.  
  766. <dd>
  767. <p>"This module matches the EUI-64 part of a stateless autoconfigured
  768. IPv6 address.  It compares the EUI-64 derived from the source MAC
  769. address in Ehternet frame with the lower 64 bits of the IPv6 source
  770. address.  But "Universal/Local" bit is not compared.  This module
  771. doesn't match other link layer frame, and is only valid in the
  772. PREROUTING, INPUT and FORWARD chains."</p>
  773. <pre>
  774.     mod eui64 ACCEPT;</pre>
  775. </dd>
  776. <dt><strong><a name="frag" class="item"><strong>frag</strong></a></strong></dt>
  777.  
  778. <dd>
  779. <p>Matches the Fragment header (ip6).</p>
  780. <pre>
  781.     mod frag fragid 123:456 ACCEPT;
  782.     mod frag fragid !123:456 ACCEPT;
  783.     mod frag fragres ACCEPT;
  784.     mod frag fragfirst ACCEPT;
  785.     mod frag fragmore ACCEPT;
  786.     mod frag fraglast ACCEPT;</pre>
  787. </dd>
  788. <dt><strong><a name="fuzzy" class="item"><strong>fuzzy</strong></a></strong></dt>
  789.  
  790. <dd>
  791. <p>"This module matches a rate limit based on a fuzzy logic controller [FLC]."</p>
  792. <pre>
  793.     mod fuzzy lower-limit 10 upper-limit 20 ACCEPT;</pre>
  794. </dd>
  795. <dt><strong><a name="hbh" class="item"><strong>hbh</strong></a></strong></dt>
  796.  
  797. <dd>
  798. <p>Matches the Hop-by-Hop Options header (ip6).</p>
  799. <pre>
  800.     mod hbh hbh-len 8 ACCEPT;
  801.     mod hbh hbh-len !8 ACCEPT;
  802.     mod hbh hbh-opts (1:4 2:8) ACCEPT;</pre>
  803. </dd>
  804. <dt><strong><a name="hl" class="item"><strong>hl</strong></a></strong></dt>
  805.  
  806. <dd>
  807. <p>Matches the Hop Limit field (ip6).</p>
  808. <pre>
  809.     mod hl hl-eq (8 10) ACCEPT;
  810.     mod hl hl-eq !5 ACCEPT;
  811.     mod hl hl-gt 15 ACCEPT;
  812.     mod hl hl-lt 2 ACCEPT;</pre>
  813. </dd>
  814. <dt><strong><a name="helper" class="item"><strong>helper</strong></a></strong></dt>
  815.  
  816. <dd>
  817. <p>Checks which conntrack helper module tracks this connection.  The port
  818. may be specified with "-portnr".</p>
  819. <pre>
  820.     mod helper helper irc ACCEPT;
  821.     mod helper helper ftp-21 ACCEPT;</pre>
  822. </dd>
  823. <dt><strong><a name="icmp" class="item"><strong>icmp</strong></a></strong></dt>
  824.  
  825. <dd>
  826. <p>Check ICMP specific attributes.  This module is automatically loaded
  827. when you use "protocol icmp".</p>
  828. <pre>
  829.     proto icmp icmp-type echo-request ACCEPT;</pre>
  830. <p>This option can also be used in be <em>ip6</em> domain, although this is
  831. called <strong>icmpv6</strong> in <em class="file">ip6tables</em>.</p>
  832. <p>Use "iptables -p icmp <code>-h</code>" to obtain a list of valid ICMP types.</p>
  833. </dd>
  834. <dt><strong><a name="iprange" class="item"><strong>iprange</strong></a></strong></dt>
  835.  
  836. <dd>
  837. <p>Match a range of IPv4 addresses.</p>
  838. <pre>
  839.     mod iprange src-range 192.168.2.0-192.168.3.255;
  840.     mod iprange dst-range ! 192.168.6.0-192.168.6.255;</pre>
  841. </dd>
  842. <dt><strong><a name="ipv4options" class="item"><strong>ipv4options</strong></a></strong></dt>
  843.  
  844. <dd>
  845. <p>Match on IPv4 header options like source routing, record route,
  846. timestamp and router-alert.</p>
  847. <pre>
  848.     mod ipv4options ssrr ACCEPT;
  849.     mod ipv4options lsrr ACCEPT;
  850.     mod ipv4options no-srr ACCEPT;
  851.     mod ipv4options !rr ACCEPT;
  852.     mod ipv4options !ts ACCEPT;
  853.     mod ipv4options !ra ACCEPT;
  854.     mod ipv4options !any-opt ACCEPT;</pre>
  855. </dd>
  856. <dt><strong><a name="ipv6header" class="item"><strong>ipv6header</strong></a></strong></dt>
  857.  
  858. <dd>
  859. <p>Matches the IPv6 extension header (ip6).</p>
  860. <pre>
  861.     mod ipv6header header !(hop frag) ACCEPT;
  862.     mod ipv6header header (auth dst) ACCEPT;</pre>
  863. </dd>
  864. <dt><strong><a name="hashlimit" class="item"><strong>hashlimit</strong></a></strong></dt>
  865.  
  866. <dd>
  867. <p>Similar to 'mod limit', but adds the ability to add per-destination or
  868. per-port limits managed in a hash table.</p>
  869. <pre>
  870.     mod hashlimit  hashlimit 10/minute  hashlimit-burst 30/minute
  871.       hashlimit-mode dstip  hashlimit-name foobar  ACCEPT;</pre>
  872. <p>Possible values for hashlimit-mode: dstip dstport srcip srcport.</p>
  873. <p>There are more possible settings, type "iptables -m hashlimit -h" for
  874. documentation.</p>
  875. </dd>
  876. <dt><strong><a name="length" class="item"><strong>length</strong></a></strong></dt>
  877.  
  878. <dd>
  879. <p>Check the package length.</p>
  880. <pre>
  881.     mod length length 128; # exactly 128 bytes
  882.     mod length length 512:768; # range
  883.     mod length length ! 256; # negated</pre>
  884. </dd>
  885. <dt><strong><a name="limit" class="item"><strong>limit</strong></a></strong></dt>
  886.  
  887. <dd>
  888. <p>Limits the packet rate.</p>
  889. <pre>
  890.     mod limit limit 1/second;
  891.     mod limit limit 15/minute limit-burst 10;</pre>
  892. <p>Type "iptables -m limit -h" for details.</p>
  893. </dd>
  894. <dt><strong><a name="mac" class="item"><strong>mac</strong></a></strong></dt>
  895.  
  896. <dd>
  897. <p>Match the source MAC address.</p>
  898. <pre>
  899.     mod mac mac-source 01:23:45:67:89;</pre>
  900. </dd>
  901. <dt><strong><a name="mark" class="item"><strong>mark</strong></a></strong></dt>
  902.  
  903. <dd>
  904. <p>Matches packets based on their netfilter mark field. This may be a 32
  905. bit integer between 0 and 4294967295.</p>
  906. <pre>
  907.     mod mark mark 42;</pre>
  908. </dd>
  909. <dt><strong><a name="mh" class="item"><strong>mh</strong></a></strong></dt>
  910.  
  911. <dd>
  912. <p>Matches the mobility header (domain <em>ip6</em>).</p>
  913. <pre>
  914.     proto mh mh-type binding-update ACCEPT;</pre>
  915. </dd>
  916. <dt><strong><a name="multiport" class="item"><strong>multiport</strong></a></strong></dt>
  917.  
  918. <dd>
  919. <p>Match a set of source or destination ports (UDP and TCP only).</p>
  920. <pre>
  921.     mod multiport source-ports (https ftp);
  922.     mod multiport destination-ports (mysql domain);</pre>
  923. <p>This rule has a big advantage over "dport" and "sport": it generates
  924. only one rule for up to 15 ports instead of one rule for every port.</p>
  925. </dd>
  926. <dt><strong><a name="nth" class="item"><strong>nth</strong></a></strong></dt>
  927.  
  928. <dd>
  929. <p>Match every 'n'th packet.</p>
  930. <pre>
  931.     mod nth every 3;
  932.     mod nth counter 5 every 2;
  933.     mod nth start 2 every 3;
  934.     mod nth start 5 packet 2 every 6;</pre>
  935. <p>Type "iptables -m nth -h" for details.</p>
  936. </dd>
  937. <dt><strong><a name="owner" class="item"><strong>owner</strong></a></strong></dt>
  938.  
  939. <dd>
  940. <p>Check information about the packet creator, namely user id, group id,
  941. process id, session id and command name.</p>
  942. <pre>
  943.     mod owner uid-owner 0;
  944.     mod owner gid-owner 1000;
  945.     mod owner pid-owner 5432;
  946.     mod owner sid-owner 6543;
  947.     mod owner cmd-owner "sendmail";</pre>
  948. <p>("cmd-owner", "pid-owner" and "sid-owner" require special kernel
  949. patches not included in the vanilla Linux kernel)</p>
  950. </dd>
  951. <dt><strong><a name="physdev" class="item"><strong>physdev</strong></a></strong></dt>
  952.  
  953. <dd>
  954. <p>Matches the physical device on which a packet entered or is about to
  955. leave the machine. This is useful for bridged interfaces.</p>
  956. <pre>
  957.     mod physdev physdev-in ppp1;
  958.     mod physdev physdev-out eth2;
  959.     mod physdev physdev-is-in;
  960.     mod physdev physdev-is-out;
  961.     mod physdev physdev-is-bridged;</pre>
  962. </dd>
  963. <dt><strong><a name="pkttype" class="item"><strong>pkttype</strong></a></strong></dt>
  964.  
  965. <dd>
  966. <p>Check the link-layer packet type.</p>
  967. <pre>
  968.     mod pkttype pkt-type unicast;
  969.     mod pkttype pkt-type broadcase;
  970.     mod pkttype pkt-type multicast;</pre>
  971. </dd>
  972. <dt><strong><a name="policy" class="item"><strong>policy</strong></a></strong></dt>
  973.  
  974. <dd>
  975. <p>Matches IPsec policy being applied to this packet.</p>
  976. <pre>
  977.     mod policy dir out pol ipsec ACCEPT;
  978.     mod policy strict reqid 23 spi 0x10 proto ah ACCEPT;
  979.     mod policy mode tunnel tunnel-src 192.168.1.2 ACCEPT;
  980.     mod policy mode tunnel tunnel-dst 192.168.2.1 ACCEPT;
  981.     mod policy strict next reqid 24 spi 0x11 ACCEPT;</pre>
  982. <p>Note that the keyword <em>proto</em> is also used as a shorthand version of
  983. <em>protocol</em> (built-in match module).  You can fix this conflict by
  984. always using the long keyword <em>protocol</em>.</p>
  985. </dd>
  986. <dt><strong><a name="psd" class="item"><strong>psd</strong></a></strong></dt>
  987.  
  988. <dd>
  989. <p>Detect TCP/UDP port scans.</p>
  990. <pre>
  991.     mod psd psd-weight-threshold 21 psd-delay-threshold 300
  992.       psd-lo-ports-weight 3 psd-hi-ports-weight 1 DROP;</pre>
  993. </dd>
  994. <dt><strong><a name="quota" class="item"><strong>quota</strong></a></strong></dt>
  995.  
  996. <dd>
  997. <p>Implements network quotas by decrementing a byte counter with each packet.</p>
  998. <pre>
  999.     mod quota quota 65536 ACCEPT;</pre>
  1000. </dd>
  1001. <dt><strong><a name="random" class="item"><strong>random</strong></a></strong></dt>
  1002.  
  1003. <dd>
  1004. <p>Match a random percentage of all packets.</p>
  1005. <pre>
  1006.     mod random average 70;</pre>
  1007. </dd>
  1008. <dt><strong><a name="realm" class="item"><strong>realm</strong></a></strong></dt>
  1009.  
  1010. <dd>
  1011. <p>Match the routing realm. Useful in environments using BGP.</p>
  1012. <pre>
  1013.     mod realm realm 3;</pre>
  1014. </dd>
  1015. <dt><strong><a name="recent" class="item"><strong>recent</strong></a></strong></dt>
  1016.  
  1017. <dd>
  1018. <p>Temporarily mark source IP addresses.</p>
  1019. <pre>
  1020.     mod recent set;
  1021.     mod recent rcheck seconds 60;
  1022.     mod recent set rsource name "badguy";
  1023.     mod recent set rdest;
  1024.     mod recent rcheck rsource name "badguy" seconds 60;
  1025.     mod recent update seconds 120 hitcount 3 rttl;</pre>
  1026. <p>This netfilter module has a design flaw: although it is implemented as
  1027. a match module, it has target-like behaviour when using the "set"
  1028. keyword.</p>
  1029. <p><a href="http://snowman.net/projects/ipt_recent/">http://snowman.net/projects/ipt_recent/</a></p>
  1030. </dd>
  1031. <dt><strong><a name="rt" class="item"><strong>rt</strong></a></strong></dt>
  1032.  
  1033. <dd>
  1034. <p>Match the IPv6 routing header (ip6 only).</p>
  1035. <pre>
  1036.     mod rt rt-type 2 rt-len 20 ACCEPT;
  1037.     mod rt rt-type !2 rt-len !20 ACCEPT;
  1038.     mod rt rt-segsleft 2:3 ACCEPT;
  1039.     mod rt rt-segsleft !4:5 ACCEPT;
  1040.     mod rt rt-0-res rt-0-addrs (::1 ::2) rt-0-not-strict ACCEPT;</pre>
  1041. </dd>
  1042. <dt><strong><a name="sctp" class="item"><strong>sctp</strong></a></strong></dt>
  1043.  
  1044. <dd>
  1045. <p>Check SCTP (Stream Control Transmission Protocol) specific attributes.
  1046. This module is automatically loaded when you use "protocol sctp".</p>
  1047. <pre>
  1048.     proto sctp sport 1234 dport 2345 ACCEPT;
  1049.     proto sctp chunk-types only DATA:Be ACCEPT;
  1050.     proto sctp chunk-types any (INIT INIT_ACK) ACCEPT;
  1051.     proto sctp chunk-types !all (HEARTBEAT) ACCEPT;</pre>
  1052. <p>Use "iptables -p sctp <code>-h</code>" to obtain a list of valid chunk types.</p>
  1053. </dd>
  1054. <dt><strong><a name="set" class="item"><strong>set</strong></a></strong></dt>
  1055.  
  1056. <dd>
  1057. <p>Checks the source or destination IP/Port/MAC against a set.</p>
  1058. <pre>
  1059.     mod set set badguys src DROP;</pre>
  1060. <p>See <a href="http://ipset.netfilter.org/">http://ipset.netfilter.org/</a> for more information.</p>
  1061. </dd>
  1062. <dt><strong><a name="state" class="item"><strong>state</strong></a></strong></dt>
  1063.  
  1064. <dd>
  1065. <p>Checks the connection tracking state.</p>
  1066. <pre>
  1067.     mod state state INVALID DROP;
  1068.     mod state state (ESTABLISHED RELATED) ACCEPT;</pre>
  1069. <p>Type "iptables -m state -h" for details.</p>
  1070. </dd>
  1071. <dt><strong><a name="statistic" class="item"><strong>statistic</strong></a></strong></dt>
  1072.  
  1073. <dd>
  1074. <p>Successor of <strong>nth</strong> and <strong>random</strong>, currently undocumented in the
  1075. <code>iptables(8)</code> man page.</p>
  1076. <pre>
  1077.     mod statistic mode random probability 0.8 ACCEPT;
  1078.     mod statistic mode nth every 5 packet 0 DROP;</pre>
  1079. </dd>
  1080. <dt><strong><strong>string</strong></strong></dt>
  1081.  
  1082. <dd>
  1083. <p>Matches a string.</p>
  1084. <pre>
  1085.     mod string string "foo bar" ACCEPT;
  1086.     mod string algo kmp from 64 to 128 hex-string "deadbeef" ACCEPT;</pre>
  1087. </dd>
  1088. <dt><strong><a name="tcp" class="item"><strong>tcp</strong></a></strong></dt>
  1089.  
  1090. <dd>
  1091. <p>Checks TCP specific attributes. This module is automatically loaded
  1092. when you use "protocol tcp".</p>
  1093. <pre>
  1094.     proto tcp sport 1234;
  1095.     proto tcp dport 2345;
  1096.     proto tcp tcp-flags (SYN ACK) SYN;
  1097.     proto tcp tcp-flags ! (SYN ACK) SYN;
  1098.     proto tcp tcp-flags ALL (RST ACK);
  1099.     proto tcp syn;
  1100.     proto tcp tcp-option 2;
  1101.     proto tcp mss 512;</pre>
  1102. <p>Type "iptables -p tcp -h" for details.</p>
  1103. </dd>
  1104. <dt><strong><a name="tcpmss" class="item"><strong>tcpmss</strong></a></strong></dt>
  1105.  
  1106. <dd>
  1107. <p>Check the TCP MSS field of a SYN or SYN/ACK packet.</p>
  1108. <pre>
  1109.     mod tcpmss mss 123 ACCEPT;
  1110.     mod tcpmss mss 234:567 ACCEPT;</pre>
  1111. </dd>
  1112. <dt><strong><a name="time" class="item"><strong>time</strong></a></strong></dt>
  1113.  
  1114. <dd>
  1115. <p>Check if the time a packet arrives is in given range.</p>
  1116. <pre>
  1117.     mod time timestart 12:00;
  1118.     mod time timestop 13:30;
  1119.     mod time days (Mon Wed Fri);
  1120.     mod time datestart 2005:01:01;
  1121.     mod time datestart 2005:01:01:23:59:59;
  1122.     mod time datestop 2005:04:01;
  1123.     mod time monthday (30 31);
  1124.     mod time weekdays (Wed Thu);
  1125.     mod time timestart 12:00 utc;
  1126.     mod time timestart 12:00 localtz;</pre>
  1127. <p>Type "iptables -m time -h" for details.</p>
  1128. </dd>
  1129. <dt><strong><a name="tos" class="item"><strong>tos</strong></a></strong></dt>
  1130.  
  1131. <dd>
  1132. <p>Matches a packet on the specified TOS-value.</p>
  1133. <pre>
  1134.     mod tos tos Minimize-Cost ACCEPT;
  1135.     mod tos tos !Normal-Service ACCEPT;</pre>
  1136. <p>Type "iptables -m tos -h" for details.</p>
  1137. </dd>
  1138. <dt><strong><a name="ttl" class="item"><strong>ttl</strong></a></strong></dt>
  1139.  
  1140. <dd>
  1141. <p>Matches the ttl (time to live) field in the IP header.</p>
  1142. <pre>
  1143.     mod ttl ttl-eq 12; # ttl equals
  1144.     mod ttl ttl-gt 10; # ttl greater than
  1145.     mod ttl ttl-lt 16; # ttl less than</pre>
  1146. </dd>
  1147. <dt><strong><a name="u32" class="item"><strong>u32</strong></a></strong></dt>
  1148.  
  1149. <dd>
  1150. <p>Compares raw data from the packet.  You can specify more than one
  1151. filter in a ferm list; these are not expanded into multiple rules.</p>
  1152. <pre>
  1153.     mod u32 u32 '6&0xFF=1' ACCEPT;
  1154.     mod u32 u32 ('27&0x8f=7' '31=0x527c4833') DROP;</pre>
  1155. </dd>
  1156. <dt><strong><a name="unclean" class="item"><strong>unclean</strong></a></strong></dt>
  1157.  
  1158. <dd>
  1159. <p>Matches packets which seem malformed or unusual. This match has no
  1160. further parameters.</p>
  1161. </dd>
  1162. </dl>
  1163. <p>
  1164. </p>
  1165. <h2><a name="iptables_target_modules">iptables target modules</a></h2>
  1166. <p>The following additional targets are available in ferm, provided that
  1167. you enabled them in your kernel:</p>
  1168. <dl>
  1169. <dt><strong><a name="classify" class="item"><strong>CLASSIFY</strong></a></strong></dt>
  1170.  
  1171. <dd>
  1172. <p>Set the CBQ class.</p>
  1173. <pre>
  1174.     CLASSIFY set-class 3:50;</pre>
  1175. </dd>
  1176. <dt><strong><a name="clusterip" class="item"><strong>CLUSTERIP</strong></a></strong></dt>
  1177.  
  1178. <dd>
  1179. <p>Configure a simple cluster of nodes that share a certain IP and MAC
  1180. address.  Connections are statically distributed between the nodes.</p>
  1181. <pre>
  1182.     CLUSTERIP new hashmode sourceip clustermac 00:12:34:45:67:89
  1183.       total-nodes 4 local-node 2 hash-init 12345;</pre>
  1184. </dd>
  1185. <dt><strong><a name="connmark" class="item"><strong>CONNMARK</strong></a></strong></dt>
  1186.  
  1187. <dd>
  1188. <p>Sets the netfilter mark value associated with a connection.</p>
  1189. <pre>
  1190.     CONNMARK set-mark 42;
  1191.     CONNMARK save-mark;
  1192.     CONNMARK restore-mark;
  1193.     CONNMARK save-mark mask 0x7fff;
  1194.     CONNMARK restore-mark mask 0x8000;</pre>
  1195. </dd>
  1196. <dt><strong><a name="connsecmark" class="item"><strong>CONNSECMARK</strong></a></strong></dt>
  1197.  
  1198. <dd>
  1199. <p>This module copies security markings from packets to connections (if
  1200. unlabeled), and from connections back to packets (also only if
  1201. unlabeled).  Typically used in conjunction with SECMARK, it is only
  1202. valid in the mangle table.</p>
  1203. <pre>
  1204.     CONNSECMARK save;
  1205.     CONNSECMARK restore;</pre>
  1206. </dd>
  1207. <dt><strong><a name="dnat_to_ip_address_ip_range_ip_port_range" class="item"><strong>DNAT to [ip-address|ip-range|ip-port-range]</strong></a></strong></dt>
  1208.  
  1209. <dd>
  1210. <p>Change the destination address of the packet.</p>
  1211. <pre>
  1212.     DNAT to 10.0.0.4;
  1213.     DNAT to 10.0.0.4:80;
  1214.     DNAT to 10.0.0.4:1024-2048;
  1215.     DNAT to 10.0.1.1-10.0.1.20;</pre>
  1216. </dd>
  1217. <dt><strong><a name="ecn" class="item"><strong>ECN</strong></a></strong></dt>
  1218.  
  1219. <dd>
  1220. <p>This target allows to selectively work around known ECN blackholes.
  1221. It can only be used in the mangle table.</p>
  1222. <pre>
  1223.     ECN ecn-tcp-remove;</pre>
  1224. </dd>
  1225. <dt><strong><a name="hl" class="item"><strong>HL</strong></a></strong></dt>
  1226.  
  1227. <dd>
  1228. <p>Modify the IPv6 Hop Limit field (ip6/mangle only).</p>
  1229. <pre>
  1230.     HL hl-set 5;
  1231.     HL hl-dec 2;
  1232.     HL hl-inc 1;</pre>
  1233. </dd>
  1234. <dt><strong><a name="ipv4optsstrip" class="item"><strong>IPV4OPTSSTRIP</strong></a></strong></dt>
  1235.  
  1236. <dd>
  1237. <p>Strip all the IP options from a packet.  This module does not take any
  1238. options.</p>
  1239. <pre>
  1240.     IPV4OPTSSTRIP;</pre>
  1241. </dd>
  1242. <dt><strong><a name="log" class="item"><strong>LOG</strong></a></strong></dt>
  1243.  
  1244. <dd>
  1245. <p>Log all packets that match this rule in the kernel log. Be carefull
  1246. with log flooding. Note that this is a "non-terminating target",
  1247. i.e. rule traversal continues at the next rule.</p>
  1248. <pre>
  1249.     LOG log-level warning log-prefix "Look at this: ";
  1250.     LOG log-tcp-sequence log-tcp-options;
  1251.     LOG log-ip-options;</pre>
  1252. </dd>
  1253. <dt><strong><a name="mark" class="item"><strong>MARK</strong></a></strong></dt>
  1254.  
  1255. <dd>
  1256. <p>Sets the netfilter mark field for the packet (a 32 bit integer between
  1257. 0 and 4294967295):</p>
  1258. <pre>
  1259.     MARK set-mark 42;
  1260.     MARK set-xmark 7/3;
  1261.     MARK and-mark 31;
  1262.     MARK or-mark 1;
  1263.     MARK xor-mark 12;</pre>
  1264. </dd>
  1265. <dt><strong><a name="masquerade" class="item"><strong>MASQUERADE</strong></a></strong></dt>
  1266.  
  1267. <dd>
  1268. <p>Masquerades matching packets. Optionally followed by a port or
  1269. port-range for iptables. Specify as "123", "123-456" or "123:456".
  1270. The port range parameter specifies what local ports masqueraded
  1271. connections should originate from.</p>
  1272. <pre>
  1273.     MASQUERADE;
  1274.     MASQUERADE to-ports 1234:2345;
  1275.     MASQUERADE to-ports 1234:2345 random;</pre>
  1276. </dd>
  1277. <dt><strong><a name="mirror" class="item"><strong>MIRROR</strong></a></strong></dt>
  1278.  
  1279. <dd>
  1280. <p>Experimental demonstration target which inverts the source and
  1281. destination fields in the IP header.</p>
  1282. <pre>
  1283.     MIRROR;</pre>
  1284. </dd>
  1285. <dt><strong><a name="netmap" class="item"><strong>NETMAP</strong></a></strong></dt>
  1286.  
  1287. <dd>
  1288. <p>Map a whole network onto another network in the <strong>nat</strong> table.</p>
  1289. <pre>
  1290.     NETMAP to 192.168.2.0/24;</pre>
  1291. </dd>
  1292. <dt><strong><a name="notrack" class="item"><strong>NOTRACK</strong></a></strong></dt>
  1293.  
  1294. <dd>
  1295. <p>Disable connection tracking for all packets matching that rule.</p>
  1296. <pre>
  1297.     proto tcp dport (135:139 445) NOTRACK;</pre>
  1298. </dd>
  1299. <dt><strong><a name="nflog" class="item"><strong>NFLOG</strong></a></strong></dt>
  1300.  
  1301. <dd>
  1302. <p>Log packets over netlink; this is the successor of <em>ULOG</em>.</p>
  1303. <pre>
  1304.     NFLOG nflog-group 5 nflog-prefix "Look at this: ";
  1305.     NFLOG nflog-range 256;
  1306.     NFLOG nflog-threshold 10;</pre>
  1307. </dd>
  1308. <dt><strong><a name="nfqueue" class="item"><strong>NFQUEUE</strong></a></strong></dt>
  1309.  
  1310. <dd>
  1311. <p>Userspace queueing, requires nfnetlink_queue kernel support.</p>
  1312. <pre>
  1313.     proto tcp dport ftp NFQUEUE queue-num 20;</pre>
  1314. </dd>
  1315. <dt><strong><a name="queue" class="item"><strong>QUEUE</strong></a></strong></dt>
  1316.  
  1317. <dd>
  1318. <p>Userspace queueing, the predecessor to <strong>NFQUEUE</strong>.  All packets go to
  1319. queue 0.</p>
  1320. <pre>
  1321.     proto tcp dport ftp QUEUE;</pre>
  1322. </dd>
  1323. <dt><strong><a name="redirect_to_ports_ports" class="item"><strong>REDIRECT to-ports [ports]</strong></a></strong></dt>
  1324.  
  1325. <dd>
  1326. <p>Transparent proxying: alter the destination IP of the packet to the
  1327. machine itself.</p>
  1328. <pre>
  1329.     proto tcp dport http REDIRECT to-ports 3128;
  1330.     proto tcp dport http REDIRECT to-ports 3128 random;</pre>
  1331. </dd>
  1332. <dt><strong><a name="same" class="item"><strong>SAME</strong></a></strong></dt>
  1333.  
  1334. <dd>
  1335. <p>Similar to SNAT, but a client is mapped to the same source IP for all
  1336. its connections.</p>
  1337. <pre>
  1338.     SAME to 1.2.3.4-1.2.3.7;
  1339.     SAME to 1.2.3.8-1.2.3.15 nodst;
  1340.     SAME to 1.2.3.16-1.2.3.31 random;</pre>
  1341. </dd>
  1342. <dt><strong><a name="secmark" class="item"><strong>SECMARK</strong></a></strong></dt>
  1343.  
  1344. <dd>
  1345. <p>This is used to set the security mark value associated with the packet
  1346. for use by security subsystems such as SELinux.  It is only valid in
  1347. the mangle table.</p>
  1348. <pre>
  1349.     SECMARK selctx "system_u:object_r:httpd_packet_t:s0";</pre>
  1350. </dd>
  1351. <dt><strong><a name="flag" class="item"><strong>SET [add-set|del-set] [setname] [flag(s)]</strong></a></strong></dt>
  1352.  
  1353. <dd>
  1354. <p>Add the IP to the specified set. See <a href="http://ipset.netfilter.org/">http://ipset.netfilter.org/</a></p>
  1355. <pre>
  1356.     proto icmp icmp-type echo-request SET add-set badguys src;</pre>
  1357. </dd>
  1358. <dt><strong><a name="snat_to_ip_address_ip_range_ip_port_range" class="item"><strong>SNAT to [ip-address|ip-range|ip-port-range]</strong></a></strong></dt>
  1359.  
  1360. <dd>
  1361. <p>Change the source address of the packet.</p>
  1362. <pre>
  1363.     SNAT to 1.2.3.4;
  1364.     SNAT to 1.2.3.4:20000-30000;
  1365.     SNAT to 1.2.3.4 random;</pre>
  1366. </dd>
  1367. <dt><strong><a name="tcpmss" class="item"><strong>TCPMSS</strong></a></strong></dt>
  1368.  
  1369. <dd>
  1370. <p>Alter the MSS value of TCP SYN packets.</p>
  1371. <pre>
  1372.     TCPMSS set-mss 1400;
  1373.     TCPMSS clamp-mss-to-pmtu;</pre>
  1374. </dd>
  1375. <dt><strong><a name="tos_set_tos_value" class="item"><strong>TOS set-tos [value]</strong></a></strong></dt>
  1376.  
  1377. <dd>
  1378. <p>Set the tcp package Type Of Service bit to this value.  This will be
  1379. used by whatever traffic scheduler is willing to, mostly your own
  1380. linux-machine, but maybe more. The original tos-bits are blanked and
  1381. overwritten by this value.</p>
  1382. <pre>
  1383.     TOS set-tos Maximize-Throughput;
  1384.     TOS and-tos 7;
  1385.     TOS or-tos 1;
  1386.     TOS xor-tos 4;</pre>
  1387. <p>Type "iptables -j TOS -h" for details.</p>
  1388. </dd>
  1389. <dt><strong><a name="ttl" class="item"><strong>TTL</strong></a></strong></dt>
  1390.  
  1391. <dd>
  1392. <p>Modify the TTL header field.</p>
  1393. <pre>
  1394.     TTL ttl-set 16;
  1395.     TTL ttl-dec 1; # decrease by 1
  1396.     TTL ttl-inc 4; # increase by 4</pre>
  1397. </dd>
  1398. <dt><strong><a name="ulog" class="item"><strong>ULOG</strong></a></strong></dt>
  1399.  
  1400. <dd>
  1401. <p>Log packets to a userspace program.</p>
  1402. <pre>
  1403.     ULOG ulog-nlgroup 5 ulog-prefix "Look at this: ";
  1404.     ULOG ulog-cprange 256;
  1405.     ULOG ulog-qthreshold 10;</pre>
  1406. </dd>
  1407. </dl>
  1408. <p>
  1409. </p>
  1410. <hr />
  1411. <h1><a name="other_domains">OTHER DOMAINS</a></h1>
  1412. <p>Since version 2.0, <strong>ferm</strong> supports not only <em>ip</em> and <em>ip6</em>, but
  1413. also <em>arp</em> (ARP tables) and <em>eb</em> (ethernet bridging tables).  The
  1414. concepts are similar to <em>iptables</em>.</p>
  1415. <p>
  1416. </p>
  1417. <h2><a name="arptables_keywords">arptables keywords</a></h2>
  1418. <dl>
  1419. <dt><strong><a name="source_ip_destination_ip" class="item"><strong>source-ip</strong>, <strong>destination-ip</strong></a></strong></dt>
  1420.  
  1421. <dd>
  1422. <p>Matches the source or destination IPv4 address.  Same as <strong>saddr</strong> and
  1423. <strong>daddr</strong> in the <em>ip</em> domain.</p>
  1424. </dd>
  1425. <dt><strong><a name="source_mac_destination_mac" class="item"><strong>source-mac</strong>, <strong>destination-mac</strong></a></strong></dt>
  1426.  
  1427. <dd>
  1428. <p>Matches the source or destination MAC address.</p>
  1429. </dd>
  1430. <dt><strong><a name="interface_outerface" class="item"><strong>interface</strong>, <strong>outerface</strong></a></strong></dt>
  1431.  
  1432. <dd>
  1433. <p>Input and output interface.</p>
  1434. </dd>
  1435. <dt><strong><a name="h_length" class="item"><strong>h-length</strong></a></strong></dt>
  1436.  
  1437. <dd>
  1438. <p>Hardware length of the packet.</p>
  1439. <pre>
  1440.     chain INPUT h-length 64 ACCEPT;</pre>
  1441. </dd>
  1442. <dt><strong><a name="opcode" class="item"><strong>opcode</strong></a></strong></dt>
  1443.  
  1444. <dd>
  1445. <p>Operation code, for details see the <code>iptables(8)</code>.</p>
  1446. <pre>
  1447.     opcode 9 ACCEPT;</pre>
  1448. </dd>
  1449. <dt><strong><a name="h_type" class="item"><strong>h-type</strong></a></strong></dt>
  1450.  
  1451. <dd>
  1452. <p>Hardware type.</p>
  1453. <pre>
  1454.     h-type 1 ACCEPT;</pre>
  1455. </dd>
  1456. <dt><strong><a name="proto_type" class="item"><strong>proto-type</strong></a></strong></dt>
  1457.  
  1458. <dd>
  1459. <p>Protocol type.</p>
  1460. <pre>
  1461.     proto-type 0x800 ACCEPT;</pre>
  1462. </dd>
  1463. <dt><strong><a name="mangling" class="item"><strong>Mangling</strong></a></strong></dt>
  1464.  
  1465. <dd>
  1466. <p>The keywords <strong>mangle-ip-s</strong>, <strong>mangle-ip-d</strong>, <strong>mangle-mac-s</strong>,
  1467. <strong>mangle-mac-d</strong>, <strong>mangle-target</strong> may be used for ARP mangling.  See
  1468. <code>iptables(8)</code> for details.</p>
  1469. </dd>
  1470. </dl>
  1471. <p>
  1472. </p>
  1473. <h2><a name="ebtables_keywords">ebtables keywords</a></h2>
  1474. <dl>
  1475. <dt><strong><a name="proto" class="item"><strong>proto</strong></a></strong></dt>
  1476.  
  1477. <dd>
  1478. <p>Matches the protocol which created the frame, e.g. <em>IPv4</em> or <strong>PPP</strong>.
  1479. For a list, see <em class="file">/etc/ethertypes</em>.</p>
  1480. </dd>
  1481. <dt><strong><a name="interface_outerface2" class="item"><strong>interface</strong>, <strong>outerface</strong></a></strong></dt>
  1482.  
  1483. <dd>
  1484. <p>Physical input and output interface.</p>
  1485. </dd>
  1486. <dt><strong><a name="logical_in_logical_out" class="item"><strong>logical-in</strong>, <strong>logical-out</strong></a></strong></dt>
  1487.  
  1488. <dd>
  1489. <p>The logical bridge interface.</p>
  1490. </dd>
  1491. <dt><strong><a name="saddr_daddr" class="item"><strong>saddr</strong>, <strong>daddr</strong></a></strong></dt>
  1492.  
  1493. <dd>
  1494. <p>Matches source or destination MAC address.</p>
  1495. </dd>
  1496. <dt><strong><a name="match_modules" class="item"><strong>Match modules</strong></a></strong></dt>
  1497.  
  1498. <dd>
  1499. <p>The following match modules are supported: 802.3, arp, ip, mark_m,
  1500. pkttype, stp, vlan, log.</p>
  1501. </dd>
  1502. <dt><strong><a name="target_extensions" class="item"><strong>Target extensions</strong></a></strong></dt>
  1503.  
  1504. <dd>
  1505. <p>The following target extensions are supported: arpreply, dnat, mark,
  1506. redirect, snat.</p>
  1507. <p>Please note that there is a conflict between <em>--mark</em> from the
  1508. <em>mark_m</em> match module and <em>-j mark</em>.  Since both would be
  1509. implemented with the ferm keyword <strong>mark</strong>, we decided to solve this by
  1510. writing the target's name in uppercase, like in the other domains.
  1511. The following example rewrites mark 1 to 2:</p>
  1512. <pre>
  1513.     mark 1 MARK 2;</pre>
  1514. </dd>
  1515. </dl>
  1516. <p>
  1517. </p>
  1518. <hr />
  1519. <h1><a name="advanced_features">ADVANCED FEATURES</a></h1>
  1520. <p>
  1521. </p>
  1522. <h2><a name="variables">Variables</a></h2>
  1523. <p>In complex firewall files, it is helpful to use variables, e.g. to
  1524. give a network interface a meaningful name.</p>
  1525. <p>To set variables, write:</p>
  1526. <pre>
  1527.     @def $DEV_INTERNET = eth0;
  1528.     @def $PORTS = (http ftp);
  1529.     @def $MORE_PORTS = ($PORTS 8080);</pre>
  1530. <p>In the real ferm code, variables are used like any other keyword
  1531. parameter:</p>
  1532. <pre>
  1533.     chain INPUT interface $DEV_INTERNET proto tcp dport $MORE_PORTS ACCEPT;</pre>
  1534. <p>Note that variables can only be used in keyword parameters
  1535. ("192.168.1.1", "http"); they cannot contain ferm keywords like
  1536. "proto" or "interface".</p>
  1537. <p>Variables are only valid in the current block:</p>
  1538. <pre>
  1539.     @def $DEV_INTERNET = eth1;
  1540.     chain INPUT {
  1541.         proto tcp {
  1542.             @def $DEV_INTERNET = ppp0;
  1543.             interface $DEV_INTERNET dport http ACCEPT;
  1544.         }
  1545.         interface $DEV_INTERNET DROP;
  1546.     }</pre>
  1547. <p>will be expanded to:</p>
  1548. <pre>
  1549.     chain INPUT {
  1550.         proto tcp {
  1551.             interface ppp0 dport http ACCEPT;
  1552.         }
  1553.         interface eth1 DROP;
  1554.     }</pre>
  1555. <p>The "def $DEV_INTERNET = ppp0" is only valid in the "proto tcp" block;
  1556. the parent block still knows "set $DEV_INTERNET = eth1".</p>
  1557. <p>Include files are special - variables declared in an included file are
  1558. still available in the calling block. This is useful when you include
  1559. a file which only declares variables.</p>
  1560. <p>
  1561. </p>
  1562. <h2><a name="automatic_variables">Automatic variables</a></h2>
  1563. <p>Some variables are set internally by ferm. Ferm scripts can use them
  1564. just like any other variable.</p>
  1565. <dl>
  1566. <dt><strong><a name="_filename" class="item"><strong>$FILENAME</strong></a></strong></dt>
  1567.  
  1568. <dd>
  1569. <p>The name of the configuration file.</p>
  1570. </dd>
  1571. <dt><strong><a name="_domain" class="item"><strong>$DOMAIN</strong></a></strong></dt>
  1572.  
  1573. <dd>
  1574. <p>The current domain.  One of <em>ip</em>, <em>ip6</em>, <em>arp</em>, <em>eb</em>.</p>
  1575. </dd>
  1576. <dt><strong><a name="_table" class="item"><strong>$TABLE</strong></a></strong></dt>
  1577.  
  1578. <dd>
  1579. <p>The current netfilter table.</p>
  1580. </dd>
  1581. <dt><strong><a name="_chain" class="item"><strong>$CHAIN</strong></a></strong></dt>
  1582.  
  1583. <dd>
  1584. <p>The current netfilter chain.</p>
  1585. </dd>
  1586. </dl>
  1587. <p>
  1588. </p>
  1589. <h2><a name="functions">Functions</a></h2>
  1590. <p>Functions are similar to variables, except that they may have
  1591. parameters, and they provide ferm commands, not values.</p>
  1592. <pre>
  1593.     @def &FOO() = proto (tcp udp) dport domain;
  1594.     &FOO() ACCEPT;</pre>
  1595. <pre>
  1596.     @def &TCP_TUNNEL($port, $dest) = {
  1597.         table filter chain FORWARD interface ppp0 proto tcp dport $port daddr $dest outerface eth0 ACCEPT;
  1598.         table nat chain PREROUTING interface ppp0 proto tcp dport $port daddr 1.2.3.4 DNAT to $dest;
  1599.     }</pre>
  1600. <pre>
  1601.     &TCP_TUNNEL(http, 192.168.1.33);
  1602.     &TCP_TUNNEL(ftp, 192.168.1.30);
  1603.     &TCP_TUNNEL((ssh smtp), 192.168.1.2);</pre>
  1604. <p>A function call which contains a block (like '{...}') must be the last
  1605. command in a ferm rule, i.e. it must be followed by ';'. The '&FOO()'
  1606. example does not contain a block, thus you may write 'ACCEPT' after
  1607. the call. To circumvent this, you can reorder the keywords:</p>
  1608. <pre>
  1609.     @def &IPSEC() = { proto (esp ah); proto udp dport 500; }
  1610.     chain INPUT ACCEPT &IPSEC();</pre>
  1611. <p>
  1612. </p>
  1613. <h2><a name="backticks">Backticks</a></h2>
  1614. <p>With backticks, you may use the output of an external command:</p>
  1615. <pre>
  1616.     @def $DNSSERVERS = `grep nameserver /etc/resolv.conf | awk '{print $2}'`;
  1617.     chain INPUT proto tcp saddr $DNSSERVERS ACCEPT;</pre>
  1618. <p>The command is executed with the shell (<em class="file">/bin/sh</em>), just like
  1619. backticks in perl.  ferm does not do any variable expansion here.</p>
  1620. <p>The output is then tokenized, and saved as a ferm list (array). Lines
  1621. beginning with '#' are ignored; the other lines may contain any number
  1622. of values, separated by whitespace.</p>
  1623. <p>
  1624. </p>
  1625. <h2><a name="includes">Includes</a></h2>
  1626. <p>The <strong>@include</strong> keyword allows you to include external files:</p>
  1627. <pre>
  1628.     @include 'vars.ferm';</pre>
  1629. <p>The file name is relative to the calling file, e.g. when including
  1630. from <em class="file">/etc/ferm/ferm.conf</em>, the above statement includes
  1631. <em class="file">/etc/ferm/vars.ferm</em>. Variables and functions declared in an
  1632. included file are still available in the calling file.</p>
  1633. <p><strong>include</strong> works within a block:</p>
  1634. <pre>
  1635.     chain INPUT {
  1636.         @include 'input.ferm';
  1637.     }</pre>
  1638. <p>If you specify a directory (with a trailing '/'), all files in this
  1639. directory are included, sorted alphabetically:</p>
  1640. <pre>
  1641.     @include 'ferm.d/';</pre>
  1642. <p>With a trailing pipe symbol, <strong>ferm</strong> executes a program and parses its
  1643. output:</p>
  1644. <pre>
  1645.     @include '/root/generate_ferm_rules.sh $HOSTNAME|'</pre>
  1646. <p>
  1647. </p>
  1648. <h2><a name="conditionals">Conditionals</a></h2>
  1649. <p>The keyword <strong>@if</strong> introduces a conditional expression:</p>
  1650. <pre>
  1651.     @if $condition DROP;</pre>
  1652. <p>A value is evaluated true just like in Perl: zero, empty list, empty
  1653. string are false, everything else is true.  Examples for true values:</p>
  1654. <pre>
  1655.     (a b); 1; 'foo'; (0 0)</pre>
  1656. <p>Examples for false values:</p>
  1657. <pre>
  1658.     (); 0; '0'; ''</pre>
  1659. <p>There is also <strong>@else</strong>:</p>
  1660. <pre>
  1661.     @if $condition DROP; @else REJECT;</pre>
  1662. <p>Note the semicolon before the <strong>@else</strong>.</p>
  1663. <p>It is possible to use curly braces after either <strong>@if</strong> or <strong>@else</strong>:</p>
  1664. <pre>
  1665.     @if $condition {
  1666.         MARK set-mark 2;
  1667.         RETURN;
  1668.     } @else {
  1669.         MARK set-mark 3;
  1670.     }</pre>
  1671. <p>Since the closing curly brace also finishes the command, there is no
  1672. need for semicolon.</p>
  1673. <p>There is no <strong>@elsif</strong>, use <strong>@else @if</strong> instead.</p>
  1674. <p>Example:</p>
  1675. <pre>
  1676.     @def $have_ipv6 = `test -f /proc/net/ip6_tables_names && echo 1 || echo`;
  1677.     @if $have_ipv6 {
  1678.         domain ip6 {
  1679.             # ....
  1680.         }
  1681.     }</pre>
  1682. <p>
  1683. </p>
  1684. <h2><a name="hooks">Hooks</a></h2>
  1685. <p>To run custom commands, you may install hooks:</p>
  1686. <pre>
  1687.     @hook pre "echo 0 >/proc/sys/net/ipv4/conf/eth0/forwarding";
  1688.     @hook post "echo 1 >/proc/sys/net/ipv4/conf/eth0/forwarding";
  1689.     @hook flush "echo 0 >/proc/sys/net/ipv4/conf/eth0/forwarding";</pre>
  1690. <p>The specified command is executed using the shell.  "pre" means run
  1691. the command before applying the firewall rules, and "post" means run
  1692. the command afterwards.  "flush" hooks are run after ferm has flushed
  1693. the firewall rules (option --flush).  You may install any number of
  1694. hooks.</p>
  1695. <p>
  1696. </p>
  1697. <hr />
  1698. <h1><a name="built_in_functions">BUILT-IN FUNCTIONS</a></h1>
  1699. <p>There are several built-in functions which you might find useful.</p>
  1700. <p>
  1701. </p>
  1702. <h2><a name="_resolve__hostname1_hostname2______">@resolve((hostname1 hostname2 ...))</a></h2>
  1703. <p>Usually, host names are resolved by iptables.  To let ferm resolve
  1704. host names, use the function @resolve:</p>
  1705. <pre>
  1706.     saddr @resolve(my.host.foo) proto tcp dport ssh ACCEPT;
  1707.     saddr @resolve((another.host.foo third.host.foo)) proto tcp dport openvpn ACCEPT;</pre>
  1708. <p>Note the double parentheses in the second line: the inner pair for
  1709. creating a ferm list, and the outer pair as function parameter
  1710. delimiters.</p>
  1711. <p>This function currently only resolves <strong>A</strong> records (i.e. IPv4
  1712. addresses), rendering it useless for the <em>ip6</em> domain.</p>
  1713. <p>Be careful with resolved host names in firewall configuration.  DNS
  1714. requests may block the firewall configuration for a long time, leaving
  1715. the machine vulnerable, or they may fail.</p>
  1716. <p>
  1717. </p>
  1718. <hr />
  1719. <h1><a name="recipes">RECIPES</a></h1>
  1720. <p>The <em class="file">./examples/</em> directory contains numerous ferm configuration
  1721. which can be used to begin a new firewall. This sections contains more
  1722. samples, recipes and tricks.</p>
  1723. <p>
  1724. </p>
  1725. <h2><a name="easy_port_forwarding">Easy port forwarding</a></h2>
  1726. <p>Ferm function make routine tasks quick and easy:</p>
  1727. <pre>
  1728.     @def &FORWARD_TCP($proto, $port, $dest) = {
  1729.         table filter chain FORWARD interface $DEV_WORLD outerface $DEV_DMZ daddr $dest proto $proto dport $port ACCEPT;
  1730.         table nat chain PREROUTING interface $DEV_WORLD daddr $HOST_STATIC proto $proto dport $port DNAT to $dest;
  1731.     }</pre>
  1732. <pre>
  1733.     &FORWARD_TCP(tcp, http, 192.168.1.2);
  1734.     &FORWARD_TCP(tcp, smtp, 192.168.1.3);
  1735.     &FORWARD_TCP((tcp udp), domain, 192.168.1.4);</pre>
  1736. <p>
  1737. </p>
  1738. <h2><a name="remote_ferm">Remote <strong>ferm</strong></a></h2>
  1739. <p>If the target machine is not able to run <strong>ferm</strong> for some reason
  1740. (maybe an embedded device without Perl), you can edit the <strong>ferm</strong>
  1741. configuration file on another computer and let <strong>ferm</strong> generate a
  1742. shell script there.</p>
  1743. <p>Example for OpenWRT:</p>
  1744. <pre>
  1745.     ferm --remote --shell mywrt/ferm.conf >mywrt/firewall.user
  1746.     chmod +x mywrt/firewall.user
  1747.     scp mywrt/firewall.user mywrt.local.net:/etc/
  1748.     ssh mywrt.local.net /etc/firewall.user</pre>
  1749. <p>
  1750. </p>
  1751. <hr />
  1752. <h1><a name="options">OPTIONS</a></h1>
  1753. <dl>
  1754. <dt><strong><a name="noexec" class="item"><strong>--noexec</strong></a></strong></dt>
  1755.  
  1756. <dd>
  1757. <p>Do not execute the <code>iptables(8)</code> commands, but skip instead. This way
  1758. you can parse your data, use <strong>--lines</strong> to view the output.</p>
  1759. </dd>
  1760. <dt><strong><a name="flush" class="item"><strong>--flush</strong></a></strong></dt>
  1761.  
  1762. <dd>
  1763. <p>Clears the firewall rules and sets the policy of all chains to ACCEPT.
  1764. <strong>ferm</strong> needs a configuration file for that to determine which domains
  1765. and tables are affected.</p>
  1766. </dd>
  1767. <dt><strong><a name="lines" class="item"><strong>--lines</strong></a></strong></dt>
  1768.  
  1769. <dd>
  1770. <p>Show the firewall lines that were generated from the rules. They
  1771. will be shown just before they are executed, so if you get error
  1772. messages from <code>iptables(8)</code> etc., you can see which rule caused
  1773. the error.</p>
  1774. </dd>
  1775. <dt><strong><a name="interactive" class="item"><strong>--interactive</strong></a></strong></dt>
  1776.  
  1777. <dd>
  1778. <p>Apply the firewall rules and ask the user for confirmation.  Reverts
  1779. to the previous ruleset if there is no valid user response within 30
  1780. seconds.  This is useful for remote firewall administration: you can
  1781. test the rules without fearing to lock yourself out.</p>
  1782. </dd>
  1783. <dt><strong><a name="help" class="item"><strong>--help</strong></a></strong></dt>
  1784.  
  1785. <dd>
  1786. <p>Show a brief list of available commandline options.</p>
  1787. </dd>
  1788. <dt><strong><a name="version" class="item"><strong>--version</strong></a></strong></dt>
  1789.  
  1790. <dd>
  1791. <p>Shows the version number of the program.</p>
  1792. </dd>
  1793. <dt><strong><a name="fast" class="item"><strong>--fast</strong></a></strong></dt>
  1794.  
  1795. <dd>
  1796. <p>Enable fast mode: ferm generates an iptables-save(8) file, and
  1797. installs it with iptables-restore(8). This is much faster, because
  1798. ferm calls <code>iptables(8)</code> once for every rule by default.</p>
  1799. <p>Fast mode is enabled by default since <strong>ferm</strong> 2.0, deprecating this
  1800. option.</p>
  1801. </dd>
  1802. <dt><strong><a name="slow" class="item"><strong>--slow</strong></a></strong></dt>
  1803.  
  1804. <dd>
  1805. <p>Disable fast mode, i.e. run <code>iptables(8)</code> for every rule, and don't use
  1806. iptables-restore(8).</p>
  1807. </dd>
  1808. <dt><strong><a name="shell" class="item"><strong>--shell</strong></a></strong></dt>
  1809.  
  1810. <dd>
  1811. <p>Generate a shell script which calls iptables-restore(8) and prints it.
  1812. Implies --fast --lines.</p>
  1813. </dd>
  1814. <dt><strong><a name="remote" class="item"><strong>--remote</strong></a></strong></dt>
  1815.  
  1816. <dd>
  1817. <p>Generate rules for a remote machine.  Implies <strong>--noexec</strong> and
  1818. <strong>--lines</strong>.  Can be combined with <strong>--shell</strong>.</p>
  1819. </dd>
  1820. <dt><strong><a name="domain_ip_ip62" class="item"><strong>--domain {ip|ip6}</strong></a></strong></dt>
  1821.  
  1822. <dd>
  1823. <p>Handle only the specified domain. <strong>ferm</strong> output may be empty if the
  1824. domain is not configured in the input file.</p>
  1825. </dd>
  1826. <dt><strong><a name="def_name_value" class="item"><strong>--def '$name=value'</strong></a></strong></dt>
  1827.  
  1828. <dd>
  1829. <p>Override a variable defined in the configuration file.</p>
  1830. </dd>
  1831. </dl>
  1832. <p>
  1833. </p>
  1834. <hr />
  1835. <h1><a name="see_also">SEE ALSO</a></h1>
  1836. <p><code>iptables(8)</code></p>
  1837. <p>
  1838. </p>
  1839. <hr />
  1840. <h1><a name="requirements">REQUIREMENTS</a></h1>
  1841. <p>
  1842. </p>
  1843. <h2><a name="operating_system">Operating system</a></h2>
  1844. <p>Linux 2.4 or newer, with netfilter support and all netfilter modules
  1845. used by your firewall script</p>
  1846. <p>
  1847. </p>
  1848. <h2><a name="software">Software</a></h2>
  1849. <p>iptables and perl 5.6</p>
  1850. <p>
  1851. </p>
  1852. <hr />
  1853. <h1><a name="bugs">BUGS</a></h1>
  1854. <p>Bugs? What bugs?</p>
  1855. <p>If you find a bug, please tell us: <a href="mailto:ferm@foo-projects.org">ferm@foo-projects.org</a></p>
  1856. <p>
  1857. </p>
  1858. <hr />
  1859. <h1><a name="copyright">COPYRIGHT</a></h1>
  1860. <p>Copyright (C) 2001-2009 Max Kellermann <<a href="mailto:max@foo-projects.org">max@foo-projects.org</a>>, Auke
  1861. Kok <<a href="mailto:sofar@foo-projects.org">sofar@foo-projects.org</a>></p>
  1862. <p>This program is free software; you can redistribute it and/or modify
  1863. it under the terms of the GNU General Public License as published by
  1864. the Free Software Foundation; either version 2 of the License, or (at
  1865. your option) any later version.</p>
  1866. <p>This program is distributed in the hope that it will be useful, but
  1867. WITHOUT ANY WARRANTY; without even the implied warranty of
  1868. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  1869. General Public License for more details.</p>
  1870. <p>You should have received a copy of the GNU General Public License
  1871. along with this program; if not, write to the Free Software
  1872. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  1873. USA</p>
  1874. <p>
  1875. </p>
  1876. <hr />
  1877. <h1><a name="author">AUTHOR</a></h1>
  1878. <p>Max Kellermann <<a href="mailto:max@foo-projects.org">max@foo-projects.org</a>>, Auke Kok
  1879. <<a href="mailto:sofar@foo-projects.org">sofar@foo-projects.org</a>></p>
  1880.  
  1881. </body>
  1882.  
  1883. </html>
  1884.